package com.spring.context;

import java.util.Map;

import com.spring.beans.BeanDefinition;
import com.spring.beans.factory.AbstractBeanFactory;
import com.spring.beans.factory.AutowireCapableBeanFactory;
import com.spring.xml.ResourceLoader;
import com.spring.xml.XmlBeanDefinitionReader;

public class ClassPathXmlApplication extends AbstractApplicationContext{

	private String configLocation;
	
	public ClassPathXmlApplication(String configLocation) {
		this(configLocation, new AutowireCapableBeanFactory());
	}
	public ClassPathXmlApplication(String configLocation,AbstractBeanFactory beanFactory) {
		super(beanFactory);
		try {
			this.configLocation = configLocation;
			refresh();
		} catch (Exception e) {
			e.printStackTrace();
		}
	}
	@Override
	protected void loadBeanDefinitions(AbstractBeanFactory beanFactory) throws Exception {
		XmlBeanDefinitionReader xmlBeanDefinitionReader = new XmlBeanDefinitionReader(new ResourceLoader());
		xmlBeanDefinitionReader.loadBeanDefinitions(configLocation);
		for (Map.Entry<String, BeanDefinition> beanDefinitionEntry : xmlBeanDefinitionReader.getRegistry().entrySet()) {
			beanFactory.registerBeanDefinition(beanDefinitionEntry.getKey(), beanDefinitionEntry.getValue());
		}
	}
}
